const Redis = require('ioredis');
const Config = require("../config/db.js");
const Sequelize = require('sequelize');


const sequelize = new Sequelize(
    Config.mysql.database,
    Config.mysql.username,
    Config.mysql.password,
    {
        host: Config.mysql.host,
        dialect: Config.mysql.dialect,
        pool: Config.mysql.pool,
        logging: Config.mysql.logging,
        timezone: '+08:00', //东八时区
        define: Config.mysql.define,
    }
)
const redis = new Redis(Config.redis);
redis.on('error', err => {
    if (err) {
        redis.quit() // 链接失败退出链接
    }
})
redis.on('ready', () => {
    console.log('Redis链接成功');
})

const db = {};

db.Sequelize = Sequelize;//引入

db.sequelize = sequelize;//实例

db.Op = Sequelize.Op; // 运算符

db.redis = redis;
db.sseClient = {};
db.models = {};
// 用户表
db.models.users = require("./sys_users.model.js")(sequelize, Sequelize);
// 菜单表
db.models.menus = require("./sys_menus.model.js")(sequelize, Sequelize);
// 角色表
db.models.roles = require("./sys_roles.model.js")(sequelize, Sequelize);
// 角色菜单表
db.models.roleMenus = require("./sys_role_menus.model.js")(sequelize, Sequelize);
db.models.roles.belongsToMany(db.models.menus, { through: db.models.roleMenus, foreignKey: 'role_id' });
db.models.menus.belongsToMany(db.models.roles, { through: db.models.roleMenus, foreignKey: 'menu_id' });
// 用户角色表
db.models.userRoles = require("./sys_user_roles.model.js")(sequelize, Sequelize);
db.models.users.belongsToMany(db.models.roles, { through: db.models.userRoles, unique: false, foreignKey: 'user_id' });
db.models.roles.belongsToMany(db.models.users, { through: db.models.userRoles, unique: false, foreignKey: 'role_id' });
// 部门表
db.models.depts = require("./sys_depts.model.js")(sequelize, Sequelize);
// 用户部门表
db.models.userDepts = require("./sys_user_depts.model.js")(sequelize, Sequelize);
db.models.users.belongsToMany(db.models.depts, { through: db.models.userDepts, foreignKey: 'user_id' });
db.models.depts.belongsToMany(db.models.users, { through: db.models.userDepts, foreignKey: 'dept_id' });
// 登录日志
db.models.loginLogs = require("./sys_login_logs.model.js")(sequelize, Sequelize);
// 定义 PureLoginLogs 与 PureUsers 之间的关系
db.models.loginLogs.belongsTo(db.models.users, { foreignKey: 'user_id', targetKey: 'id' });
db.models.users.hasMany(db.models.loginLogs, { foreignKey: 'user_id', sourceKey: 'id' });
// 附件表
db.models.files = require("./sys_files.model.js")(sequelize, Sequelize);
db.models.files.belongsTo(db.models.users, { foreignKey: 'user_id', targetKey: 'id' });
db.models.users.hasMany(db.models.files, { foreignKey: 'user_id', sourceKey: 'id' });
// 通知表
db.models.notices = require("./sys_notices.model.js")(sequelize, Sequelize);
db.models.notices.belongsTo(db.models.users, { as: 'fromUser', foreignKey: 'from_user' });
db.models.notices.belongsTo(db.models.users, { as: 'toUser', foreignKey: 'to_user' });
// 操作日志表
db.models.opLogs = require("./sys_operation_logs.model.js")(sequelize, Sequelize);
db.models.opLogs.belongsTo(db.models.users, { foreignKey: 'user_id', targetKey: 'id' });
db.models.users.hasMany(db.models.opLogs, { foreignKey: 'user_id', sourceKey: 'id' });

module.exports = db;